package com.rs2hd.packethandler;

import com.rs2hd.Constants;
import com.rs2hd.model.Player;
import com.rs2hd.model.World;
import com.rs2hd.net.Packet;
import com.rs2hd.util.Misc;
import org.apache.mina.common.IoSession;
import com.rs2hd.model.WalkingQueue;

public class PlayerPacketHandler implements PacketHandler {
	private transient WalkingQueue walkingQueue;

	@Override
	public void handlePacket(Player player, IoSession session, Packet packet) {
		switch (packet.getId()) {
		case 140:
			/*
			 * Option 1.
			 */
			option1(player, session, packet);
			break;
		case 114:
			/*
			 * Option 2.
			 */
			option2(player, session, packet);
			break;
		case 79:
			/*
			 * Magic On Player
			 */
			magicOnPlayer(player, session, packet);
			break;
		case 0:
			/*
			 * Option 3.
			 */
			option3(player, session, packet);
			break;
		}
	}

		public WalkingQueue getWalkingQueue() {
			return walkingQueue;
		}

	private void magicOnPlayer(Player player, IoSession session, Packet packet) {
		packet.readByte();
		int interfaceId = packet.readInt2() & 0xFFFF;
		int spellId = packet.readLEShort() & 0xFFFF;
		int playerId = packet.readShortA();
		player.getMagic().handleMagic(playerId, interfaceId, spellId);
	}
	private void option1(final Player player, IoSession session, Packet packet) {
		int id = packet.readLEShort() & 0xFFFF;
		if(id < 0 || id >= Constants.PLAYER_CAP) {
			return;
		}
		final Player victim = World.getInstance().getPlayerList().get(id);
		player.turnTemporarilyTo(victim);
		player.getWalkingQueue().GoTo(player, victim.getLocation().getX(), victim.getLocation().getY());
		if (player.getCombat().usingRange()) {
			player.getWalkingQueue().reset();
		
		}
		player.setEnemy(id);
		player.setAttacking(true);
	}

	private void option2(final Player player, IoSession session, Packet packet) {
		int id = packet.readShort();
		Player other = World.getInstance().getPlayerList().get(id);
		if(other == null) {
			return;
		}
		player.getRequests().requestTrade(other);
	}
	private void option3(final Player player, IoSession session, Packet packet) {
		if (player.following && player.combat.attacking) {
final Player victim = World.getInstance().getPlayerList().get(player.followIndex);
		player.getWalkingQueue().GoTo(player, victim.getLocation().getX(), victim.getLocation().getY());
			}
			
                    }

               }
